package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.ts.api.http.org.entity.apply.OrgApplyZhongYing;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 安心-众赢-河池 单公35
 * 链接：https://www.kdocs.cn/l/clFvJ78o98DN
 * @author gwc
 * @create 2025-08-26 上午9:37
 * PS:文档中有需要说重排序，实际情况进件中不需要重排序，实体类直接转json加密发送即可
 */
@ApiPlan(productId = "424", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5ZhongYing implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "KytchC5B1Axfq35B";
    private static final String IV = "Ky2YDTIOAbMVIjBg";
    private static final String CHANNEL_SIGN = "KytchC5B1Axfq35B";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {

        Map<Object, Object> phone = new LinkedHashMap<>();
        phone.put("\"phone_md5\"", apiDataExchangeParameter.getPhoneMd5());
        // 加密
        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(phone));
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("channelSign", CHANNEL_SIGN);
            newMap.put("data", data);
        }));
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        // 创建对应的进件实体类
        OrgApplyZhongYing zhongYingData = new OrgApplyZhongYing();
        // 1:客户基本信息
        // 1.1:姓名
        zhongYingData.getCustomerInfo().setName(apiApplyParameter.getUserName());
        // 1.2:性别(1.男，2.女)
        Integer sexChoose = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end(1);
        zhongYingData.getCustomerInfo().setGender(sexChoose);
        // 1.3：城市名称
        zhongYingData.getCustomerInfo().setCityName(apiApplyParameter.getCity());
        // 1.4：学历: 1-高中及以下, 2-大专/本科, 3-硕士及以上
        Integer eduChoose = ChooseEq.create()
                .when(apiApplyParameter.getEducationAttainment(), "1", 1)
                .when(apiApplyParameter.getEducationAttainment(), "2", 1)
                .when(apiApplyParameter.getEducationAttainment(), "3", 1)
                .when(apiApplyParameter.getEducationAttainment(), "4", 2)
                .when(apiApplyParameter.getEducationAttainment(), "5", 2)
                .when(apiApplyParameter.getEducationAttainment(), "6", 3)
                .end(1);
        zhongYingData.getCustomerInfo().setEducation(eduChoose);
        // 1.5：职业类型: 1-自雇人士, 2-工薪族, 3-三无, 4-其他
        Integer careerChoose = ChooseEq.create()
                .when(apiApplyParameter.getCareer(), "1", 2)
                .when(apiApplyParameter.getCareer(), "2", 1)
                .when(apiApplyParameter.getCareer(), "3", 2)
                .when(apiApplyParameter.getCareer(), "4", 2)
                .when(apiApplyParameter.getCareer(), "5", 1)
                .end(3);
        zhongYingData.getCustomerInfo().setJobType(careerChoose);
        // 1.6：年龄
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        zhongYingData.getCustomerInfo().setAge(apiFieldConverter.getAgeInt());
        // 1.7：手机号
        zhongYingData.getCustomerInfo().setPhoneNumber(apiApplyParameter.getPhone());
        // 1.8：手机号MD5
        zhongYingData.getCustomerInfo().setPhoneMd5(apiApplyParameter.getPhoneMd5());
        // 2：客户需求信息
        // 2.1：贷款金额（1. 0-3万，2. 3-5万，3. 10-20万，4. 20万以上，5. 1万-5万，6. 1万以内，7. 其他）
        Integer loanAmountChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 1)
                .when(apiApplyParameter.getLoanAmount(), "2", 2)
                .when(apiApplyParameter.getLoanAmount(), "3", 3)
                .when(apiApplyParameter.getLoanAmount(), "4", 4)
                .end(1);
        zhongYingData.getCustomerRequirements().setLoanAmount(loanAmountChoose);
        // 2.2：贷款用途（1. 消费，2. 装修，3. 购车买房，4. 经营周转，5. 其他）
        Integer loanPurposeChoose = ChooseEq.create()
                .when(apiApplyParameter.getLoanPurpose(), "1", 1)
                .when(apiApplyParameter.getLoanPurpose(), "2", 1)
                .when(apiApplyParameter.getLoanPurpose(), "3", 1)
                .when(apiApplyParameter.getLoanPurpose(), "4", 2)
                .when(apiApplyParameter.getLoanPurpose(), "5", 1)
                .when(apiApplyParameter.getLoanPurpose(), "6", 1)
                .end(5);
        zhongYingData.getCustomerRequirements().setLoanPurpose(loanPurposeChoose);
        // 3：客户详细信息
        // 3.1：银行代发
        String salaryPayMethodChoose = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", "银行代发打卡")
                .when(apiApplyParameter.getSalaryPayMethod(), "2", "现金收入")
                .when(apiApplyParameter.getSalaryPayMethod(), "3", "个人转账")
                .end();
        zhongYingData.getCustomerDetails().setBankPayroll(salaryPayMethodChoose);
        // 3.2：社保状态（0. 无，1. 有）
        String socialSecurityChoose = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", "0")
                .when(apiApplyParameter.getSocialSecurity(), "2", "1")
                .when(apiApplyParameter.getSocialSecurity(), "3", "1")
                .when(apiApplyParameter.getSocialSecurity(), "4", "1")
                .end("0");
        zhongYingData.getCustomerDetails().setSocialSecurityStatus(socialSecurityChoose);
        // 3.3：公积金 （0. 无，1. 有）
        Integer providentFundChoose = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(0);
        zhongYingData.getCustomerDetails().setProvidentFundStatus(providentFundChoose);
        // 4：客户资产信息
        // 4.1: 打卡工资: 0-无打卡工资, 1-1万及以下, 2-1.1万~2万, 3-2.1万~5万, 4-5.1万及以上
        Integer monthlySalaryFundChoose = ChooseEq.create()
                .when(apiApplyParameter.getMonthlySalary(), "1", 1)
                .when(apiApplyParameter.getMonthlySalary(), "2", 1)
                .when(apiApplyParameter.getMonthlySalary(), "3", 2)
                .when(apiApplyParameter.getMonthlySalary(), "4", 2)
                .when(apiApplyParameter.getMonthlySalary(), "5", 3)
                .end(0);
        zhongYingData.getCustomerAssets().setSalaryRange(monthlySalaryFundChoose);
        // 4.2：房产（1有/0无）
        Integer realEstateInfoChoose = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        zhongYingData.getCustomerAssets().setHouseProperty(realEstateInfoChoose);
        // 4.3：车产（1有/0无）
        Integer carInfoChoose = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end();
        zhongYingData.getCustomerAssets().setCarProperty(carInfoChoose);
        // 4.4：保单（1有/0无）
        Integer insurancePolicyChoose = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end();
        zhongYingData.getCustomerAssets().setBdStatus(insurancePolicyChoose);
        // 4.5：花呗白条额度
        String btLimitChoose = ChooseEq.create()
                .when(apiApplyParameter.getBtLimit(), "1", "0")
                .when(apiApplyParameter.getBtLimit(), "2", "3000")
                .when(apiApplyParameter.getBtLimit(), "3", "10000")
                .when(apiApplyParameter.getBtLimit(), "4", "20000")
                .when(apiApplyParameter.getBtLimit(), "5", "30000")
                .end("0");
        zhongYingData.getCustomerAssets().setHuabeiBaitiao(btLimitChoose);
        // 4.5：花呗白条额度
        String hbLimitChoose = ChooseEq.create()
                .when(apiApplyParameter.getHbLimit(), "1", "0")
                .when(apiApplyParameter.getHbLimit(), "2", "3000")
                .when(apiApplyParameter.getHbLimit(), "3", "10000")
                .when(apiApplyParameter.getHbLimit(), "4", "20000")
                .when(apiApplyParameter.getHbLimit(), "5", "30000")
                .end("0");
        zhongYingData.getCustomerAssets().setHuabeiBaitiao(hbLimitChoose);
        // 4.6： 芝麻信用分
        Integer sesameCreditChoose = ChooseEq.create()
                .when(apiApplyParameter.getSesameCredit(), "1", 600)
                .when(apiApplyParameter.getSesameCredit(), "2", 649)
                .when(apiApplyParameter.getSesameCredit(), "3", 699)
                .when(apiApplyParameter.getSesameCredit(), "4", 700)
                .end();
        zhongYingData.getCustomerAssets().setSesameCreditScore(sesameCreditChoose);
        // 5：客户征信信息
        // 5.1：有无逾期（0.无，1.有）
        Integer overdueChoose = ChooseEq.create()
                .when(apiApplyParameter.getOverdue(), "1", 0)
                .when(apiApplyParameter.getOverdue(), "2", 1)
                .when(apiApplyParameter.getOverdue(), "3", 0)
                .end(1);
        zhongYingData.getCreditInformation().setCreditOverdue(overdueChoose);
        // 6：信贷信息
        // 6.1：信用卡总金额
        String creditLimit = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", "0")
                .when(apiApplyParameter.getCreditLimit(), "2", "3000")
                .when(apiApplyParameter.getCreditLimit(), "3", "10000")
                .when(apiApplyParameter.getCreditLimit(), "4", "30000")
                .when(apiApplyParameter.getCreditLimit(), "5", "35000")
                .when(apiApplyParameter.getCreditLimit(), "6", "3000")
                .end("0");
        zhongYingData.getCreditInformation().setCreditCardTotalAmount(creditLimit);

        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes());
        String data = aes.encryptToBase64Str(JsonUtils.toJson(zhongYingData));
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("channelSign", CHANNEL_SIGN);
            newMap.put("data", data);
        }));
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
